package cn.home.bootstrap.workqueues;

        import com.google.common.collect.Lists;
        import com.rabbitmq.client.Channel;
        import com.rabbitmq.client.Connection;
        import com.rabbitmq.client.ConnectionFactory;
        import com.rabbitmq.client.MessageProperties;
        import lombok.extern.slf4j.Slf4j;

        import java.util.Arrays;
        import java.util.List;


/**
 * 一个生产者，多个消费者，生产者
 *  发送端和场景1不同点：
 * 1、使用“task_queue”声明了另一个Queue，因为RabbitMQ不容许声明2个相同名称、配置不同的Queue
 * 2、使"task_queue"的Queue的durable的属性为true，即使消息队列durable
 * 3、使用MessageProperties.PERSISTENT_TEXT_PLAIN使消息durable
 * When RabbitMQ quits or crashes it will forget the queues and messages unless you tell it not to.
 * Two things are required to make sure that messages aren't lost: we need to mark both the queue and messages as durable.
 */
@Slf4j
public class NewTask {
  private final static String IP = "127.0.0.1";
  private final static int  PORT = 5672;
  private static final String TASK_QUEUE_NAME = "task_queue";

  public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(IP);
    factory.setPort(PORT);
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();
    //队列持久化
    channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
    List<String>  messages = Arrays.asList("com.java","org.groovy","cn.home","top.google","down.up","go.back");
    for(String message : messages){
      //消息持久化
      channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes("UTF-8"));
      log.info(" [x] Sent '{}'", message);
    }
   /* channel.close();
    connection.close();*/
  }
}
